<template>
  <div>
    <!-- 面包屑导航视图 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 第一行视图包括搜索框和添加商品按钮 -->
      <el-row :gutter="20">
        <!-- 搜索框视图区域 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList" @change="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <!-- 按钮视图区域 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格视图 -->
      <el-table :data="goodsList" style="width: 100%" stripe border>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="价格(元)" width="95px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="95px"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="searchGoods(scope.row.goods_id)"></el-button>
            <!-- 删除按钮 -->
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeGood(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部页码视图 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[7, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 修改商品的dialog视图 -->
    <el-dialog title="商品修改" :visible.sync="editGoodsVisible" width="50%" @close="editDialogClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editGoodsVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 存储搜索框的数据，和请求商品列表的数据
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 7
      },
      // 存储获取到的所有商品数据
      goodsList: [],
      // 存储数据总共有多少条
      total: 0,
      // 存储将要修改的商品的数据
      editForm: {},
      // 控制修改商品页面的显示与隐藏
      editGoodsVisible: false,
      // 商品修改的验证规则
      editFormRules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }],
        goods_weight: [{ required: true, message: '请输入商品重量', trigger: 'blur' }]
      },
      // 将要被修改的商品id
      editId: ''
    }
  },
  created() {
    // 在页面成功渲染以前查询list表格
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('请求商品列表失败！')
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 当底部页面的当前显示多少条发生改变的时候触发的函数
    handleSizeChange(newPagesize) {
      this.queryInfo.pagesize = newPagesize
      this.getGoodsList()
    },
    // 当底部页面的当前页码发生改变的时候触发的函数
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getGoodsList()
    },
    // 删除商品的处理函数
    async removeGood(goodsId) {
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已经取消删除操作')
      const { data: res } = await this.$http.delete('goods/' + goodsId)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.getGoodsList()
      return this.$message.success('删除商品成功')
    },
    // 点击添加商品按钮跳转页面
    goAddPage() {
      this.$router.push('/goods/add')
    },
    // 点击修改商品按钮查询商品
    async searchGoods(goodsId) {
      // 储存要修改的商品id
      this.editId = goodsId
      const { data: res } = await this.$http.get('goods/' + goodsId)
      if (res.meta.status !== 200) return this.$message.error('商品查询失败')
      console.log(res.data)
      this.editForm = res.data
      // 显示修改dialog视图
      this.editGoodsVisible = true
      return this.$message.success('商品查询成功')
    },
    // 监听弹出框关闭事件
    editDialogClose() {
      this.$refs.editFormRef.resetFields()
    },
    // 点击确定向远程服务器发起修改，并关闭弹出框
    editGoods() {
      // 首先要对表单进行预验证
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return this.$message.info('含有表单项验证未通过')
        const { data: res } = await this.$http.put('goods/' + this.editId, this.editForm)
        if (res.meta.status !== 200) return this.$message.error('修改商品失败')
        this.getGoodsList()
        this.editGoodsVisible = false
        return this.$message.success('修改商品成功')
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
